package com.lxmes.sales.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.NcPurchaseOrderTotalMapper;
import com.lxmes.sales.domain.NcPurchaseOrderTotal;
import com.lxmes.sales.service.INcPurchaseOrderTotalService;

/**
 * 采购订单总Service业务层处理
 * 
 * @author lx
 * @date 2023-06-06
 */
@Service
public class NcPurchaseOrderTotalServiceImpl implements INcPurchaseOrderTotalService 
{
    @Autowired
    private NcPurchaseOrderTotalMapper ncPurchaseOrderTotalMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询采购订单总
     * 
     * @param id 采购订单总主键
     * @return 采购订单总
     */
    @Override
    public NcPurchaseOrderTotal selectNcPurchaseOrderTotalById(Long id)
    {
        return ncPurchaseOrderTotalMapper.selectNcPurchaseOrderTotalById(id);
    }

    /**
     * 查询采购订单总列表
     * 
     * @param ncPurchaseOrderTotal 采购订单总
     * @return 采购订单总
     */
    @Override
    public List<NcPurchaseOrderTotal> selectNcPurchaseOrderTotalList(NcPurchaseOrderTotal ncPurchaseOrderTotal)
    {
        return ncPurchaseOrderTotalMapper.selectNcPurchaseOrderTotalList(ncPurchaseOrderTotal);
    }

    /**
     * 新增采购订单总
     * 
     * @param ncPurchaseOrderTotal 采购订单总
     * @return 结果
     */
    @Override
    public int insertNcPurchaseOrderTotal(NcPurchaseOrderTotal ncPurchaseOrderTotal)
    {
        ncPurchaseOrderTotal.setCreateTime(DateUtils.getNowDate());
        return ncPurchaseOrderTotalMapper.insertNcPurchaseOrderTotal(ncPurchaseOrderTotal);
    }

    /**
     * 修改采购订单总
     * 
     * @param ncPurchaseOrderTotal 采购订单总
     * @return 结果
     */
    @Override
    public int updateNcPurchaseOrderTotal(NcPurchaseOrderTotal ncPurchaseOrderTotal)
    {
        ncPurchaseOrderTotal.setUpdateTime(DateUtils.getNowDate());
        return ncPurchaseOrderTotalMapper.updateNcPurchaseOrderTotal(ncPurchaseOrderTotal);
    }

    /**
     * 批量删除采购订单总
     * 
     * @param ids 需要删除的采购订单总主键
     * @return 结果
     */
    @Override
    public int deleteNcPurchaseOrderTotalByIds(Long[] ids)
    {
        return ncPurchaseOrderTotalMapper.deleteNcPurchaseOrderTotalByIds(ids);
    }

    /**
     * 删除采购订单总信息
     * 
     * @param id 采购订单总主键
     * @return 结果
     */
    @Override
    public int deleteNcPurchaseOrderTotalById(Long id)
    {
        return ncPurchaseOrderTotalMapper.deleteNcPurchaseOrderTotalById(id);
    }
    @Override
    public String imporIteminfo(List<NcPurchaseOrderTotal> ncPurchaseOrderTotalList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncPurchaseOrderTotalList) || ncPurchaseOrderTotalList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPurchaseOrderTotal ncPurchaseOrderTotal : ncPurchaseOrderTotalList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncPurchaseOrderTotal);
                ncPurchaseOrderTotal.setCreateBy(operName);
                ncPurchaseOrderTotal.setUpdateBy(operName);
                this.insertNcPurchaseOrderTotal(ncPurchaseOrderTotal);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
